import java.awt.event.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Entry point.  Starts the server and listens on port 4444 or the
 * first argument passed in.
 */
class Demo extends LocalServer {

  /**
   * Returns a '|' delimited list of the files in the user's home directory
   */
  private class HomeDirectoryHandler extends AbstractHandler {

    public String handle(Map<String,String> args) {
      File d = new File(System.getProperty("user.home"));
      File fs[] = d.listFiles();
      StringBuffer res = new StringBuffer();
      for (File f : fs) res.append(f).append("|");
      return res.toString();
    }

    HomeDirectoryHandler() {
      super("HomeDir");
    }
  }

  /**
   * Returns a '|' delimited list of system info
   */
  private class SystemInfoHandler extends AbstractHandler {
    
    public String handle(Map<String,String> args) {
      StringBuffer res = new StringBuffer();
      Runtime rt = Runtime.getRuntime();
      Properties props = new Properties();
      props.put("Max memory",String.valueOf(rt.maxMemory()));
      props.put("Total memory",String.valueOf(rt.totalMemory()));
      props.put("Free memory",String.valueOf(rt.freeMemory()));
      props.put("# processors",String.valueOf(rt.availableProcessors()));
      return formatProps(props);
    }

    SystemInfoHandler() {
      super("SystemInfo");
    }
  }

  /**
   * Returns a '|' delimited list of system info
   */
  private class VMInfoHandler extends AbstractHandler {
    
    public String handle(Map<String,String> args) {
      StringBuffer res = new StringBuffer();
      Properties props = new Properties();
      try {
        List<String> names = new ArrayList<String>();
        Class cls = Class.forName("sun.management.ManagementFactory");
        addProps(cls.getDeclaredField("jvm"),props);
        for (Field f : cls.getDeclaredFields()) {
          if (f.getName().endsWith("Bean")) {
            addProps(f,props);
          }
        }
      } catch (Throwable e) {e.printStackTrace();}
      return formatProps(props);
    }

    VMInfoHandler() {
      super("VMInfo");
    }
  }

  /**
   * Returns a '|' delimited list of Java proprties
   */
  private class JavaPropsHandler extends AbstractHandler {
    
    public String handle(Map<String,String> args) {
      return formatProps(System.getProperties());
    }

    JavaPropsHandler() {
      super("JavaProps");
    }
  }

  /**
   * Invokes the command specified by 'cmd'.
   */
  private class ShellHandler extends AbstractHandler {
    
    public String handle(Map<String,String> args) {
      String cmdArg = args.get("cmd");
      System.out.println(cmdArg);
      String[] cmd = cmdArg.split("%20");
      for (int i=0; i<cmd.length; i++) {
        cmd[i] = URLDecoder.decode(cmd[i]);
      }
      return invoke(cmd);
    }

    ShellHandler() {
      super("Shell");
    }
  }

  /**
   * Returns the 'x' coordinate of the AMSTracker.
   */
  private class AMSTrackerHandler extends AbstractHandler {

    private int x;

    AMSTrackerHandler() {
      super("AMS");
      final Control c = new Control();
      new Thread(new Runnable() {
          public void run() {
            try {
              c.start(new Control.Callback() {
                  public void process(int x, int y, int z) {
                    AMSTrackerHandler.this.x = x;
                  }
                });
            } catch (Exception e) {e.printStackTrace();}
          }}).start();
    }

    public String handle(Map<String,String> args) {
      return String.valueOf(x);
    }
  }


  Handler[] getHandlers() {
    return new Handler[] {
      new HomeDirectoryHandler(),
      new SystemInfoHandler(),
      new JavaPropsHandler(),
      new VMInfoHandler(),
      new ShellHandler(),
      new AMSTrackerHandler(),
    };
  }


  public static void main(String args[]) {
    new Demo().realMain(args);
  }
  
}
